package iosr11.Service;

import iosr11.data.ProjectGroup;
import iosr11.data.ProjectSubject;
import iosr11.gbase.dao.ProjectGroupDAO;
import iosr11.gbase.dao.ProjectSubjectDAO;

import java.util.List;
import java.util.logging.Logger;

public class ProjectSubjectServiceImpl implements ProjectSubjectService {
	
	private ProjectGroupDAO projectGroupDAO;
	private ProjectSubjectDAO projectSubjectDAO;
	private Logger log = Logger.getLogger("service-log");
	
	
	
	@Override
	public Class<ProjectSubject> getServiceEntityClass() {
		return ProjectSubject.class;
	}

	@Override
	public ProjectSubject getSubject(String title) {
		if (title==null){
			log.warning("nie podano tematu grupy");
			return null;
		}
		ProjectSubject subject = projectSubjectDAO.getByTopic(title);
		if (subject==null){
			log.info("nie znaleziono grupy do tematu: "+title);
			return null;
		}
		if (subject.getGroup()!=null){
			subject.setGroup(projectGroupDAO.getByID(subject.getGroup().getId()));
		}
		return subject;
	}

	@Override
	public ProjectSubject getSubject(ProjectGroup group) {
		if (group==null){
			log.info("brak grupy");
			return null;
		}
		if (group.getSubject()==null){
			log.info("brak ref na temat - szukam w bazie");
			ProjectGroup grp = projectGroupDAO.getByID(group.getId());
			if (grp==null){
				log.info("brak grupy w bazie");
				return null;
			}
			if (grp.getSubject()==null){
				log.info("brak tematu - koniec");
				return null;
			}
			group=grp;
		}
		return this.getProjectSubject(group.getSubject().getId());
	}

	@Override
	public ProjectSubject getProjectSubject(String id) {
		ProjectSubject subject = projectSubjectDAO.getByID(id);
		if (subject.getGroup()!=null){
			subject.setGroup(projectGroupDAO.getByID(subject.getGroup().getId()));
		}
		return subject;
	}

	@Override
	public boolean addProjectSubject(ProjectSubject subject) {
		log.info("dodawanie tematu");
		return projectSubjectDAO.add(subject);
	}

	@Override
	public boolean deleteProjectSubject(ProjectSubject subject) {
		// TODO Auto-generated method stub
		// brak usuwania referencji z grupy
		log.info("usuwanie tematu");
		return projectSubjectDAO.delete(subject);
	}

	private void findGroupForSubject(ProjectSubject subject){
		if (subject.getGroup()!=null)
			if (subject.getGroup().getId()!=null){
				log.info("znaleziono rupe do tematu");
				subject.setGroup(projectGroupDAO.getByID(subject.getGroup().getId()));
				subject.getGroup().setSubject(subject);
			}
	}
	
	@Override
	public List<ProjectSubject> getAll() {
		// TODO Auto-generated method stub
		// brak pobierania grup dla kazdego z tematow
		log.info("pobiera wszystkie tematy");
		List<ProjectSubject> lista = projectSubjectDAO.getAll();
		if (lista ==null){
			log.info("brak tematow -null");
			return null;
		}
		for (ProjectSubject grp : lista){
			this.findGroupForSubject(grp);
		}
		return lista;
	}

	@Override
	public List<ProjectSubject> getFreeSubjects() {
		List<ProjectSubject> lista = this.getAll();
		if (lista ==null){
			log.info("brak tematow -null");
			return null;
		}
		int i=0;
		while (i<lista.size()){
			if (lista.get(i).getGroup()!=null){
				lista.remove(i);
			}
			else {
				i++;
			}
		}
		return lista;
	}

	@Override
	public boolean isSubjectFree(ProjectSubject subject) {
		if (subject==null){
			log.warning(("nie podano tematu"));
			return false;
		}
		if (subject.getId()==null){
			log.warning("brak id do tematu");
			return false;
		}
		ProjectSubject tmp = projectSubjectDAO.getByID(subject.getId());
		if (tmp.getGroup()==null){
			log.info("do tematu nie jest przypisana grupa");
			return true;
		}
		else {
			log.info("temat zajety");
			return false;
		}
	}

	

	/* ============================================
	 * Settery dla springa
	 * ============================================
	 */
	

	public void setProjectGroupDAO(ProjectGroupDAO projectGroupDAO) {
		this.projectGroupDAO = projectGroupDAO;
	}

	public void setProjectSubjectDAO(ProjectSubjectDAO projectSubjectDAO) {
		this.projectSubjectDAO = projectSubjectDAO;
	}

}
